# SPDX-FileCopyrightText: 2018-2020 nic.at GmbH, 2025 Institute for Common Good Technology
#
# SPDX-License-Identifier: AGPL-3.0-or-later
# bash completion for intelmqctl                            -*- shell-script -*-

_intelmqctl ()
{
    local cur prev opts base;
    COMPREPLY=();
    cur="${COMP_WORDS[COMP_CWORD]}";
    prev="${COMP_WORDS[COMP_CWORD-1]}";

    generic_pre="-h --help -v --version"
    generic_post="--quiet --type"

    if [[ "$prev" == -t ]] || [[ "$prev" == --type ]]; then
        COMPREPLY=( $( compgen -W "json text"  -- "$cur" ) )
        return 0
    fi

    which yq >& /dev/null
    misses_yq=$?

    #echo "posice: $COMP_CWORD $COMP_WORDS";
    case $COMP_CWORD in
        1)
            opts="start stop restart reload run status log clear list check enable disable upgrade-config debug";
            COMPREPLY=($(compgen -W "${opts} ${generic_pre} ${generic_post}" -- ${cur}));
            return 0
        ;;
        2)
            runtime='/opt/intelmq/etc/runtime.yaml';
		    [ -f ${runtime} ] || runtime='/etc/intelmq/runtime.yaml';
            case "${COMP_WORDS[1]}" in
                start | stop | restart | status | reload | log | run | enable | disable)
                    [[ "$misses_yq" -eq 1 ]] && return 0
                    local bots=$(yq 'keys[]' $runtime | grep -v '^global$');
                    COMPREPLY=($(compgen -W "${bots}" -- ${cur}));
                    return 0
                ;;
                clear)
                    [[ "$misses_yq" -eq 1 ]] && return 0
                    local destination_queues=$(yq '.[] | .["parameters"] | .["destination_queues"] | .[] | .[]' $runtime);
                    local source_queues=$(yq 'keys[]' $runtime | grep -v '^global$' | while read line; do echo "$line-queue"; done)
                    COMPREPLY=($(compgen -W "${source_queues} ${destination_queues}" -- ${cur}));
                    return 0
                ;;
                list)
                    COMPREPLY=($(compgen -W "bots queues queues-and-status" -- ${cur}));
                    return 0
                ;;
                #*)
                #    COMPREPLY=($(compgen -W "${generic_post}" -- ${cur}));
                #    return 0
                #;;
            esac
        ;;
        3)
            case "${COMP_WORDS[1]}" in
                run)
                    COMPREPLY=($(compgen -W "console message process" -- ${cur}));
                    return 0
                ;;
                #*)
                #    COMPREPLY=($(compgen -W "${generic_post}" -- ${cur}));
                #    return 0
                #;;
            esac
        ;;
        4)
            case "${COMP_WORDS[1]}" in
                log)
                    COMPREPLY=( $( compgen -W "DEBUG INFO WARNING ERROR CRITICAL"  -- "$cur" ) )
                    return 0
                ;;
                run)
                    case "${COMP_WORDS[3]}" in
                        console)
                            local consoles=$(pip3 list 2>/dev/null | grep -e 'pdb\|pudb' | cut -d' ' -f1)
                            COMPREPLY=($(compgen -W "pdb ${consoles}" -- ${cur}));
                            return 0
                        ;;
                        message)
                            COMPREPLY=($(compgen -W "get pop send" -- ${cur}));
                            return 0
                        ;;
                        process)
                            COMPREPLY=($(compgen -W "--show-sent --dry-run --msg" -- ${cur}));
                            return 0
                        ;;
                    esac
                ;;
            esac
        ;;
        5)
            if [[ "${COMP_WORDS[1]}" == "run" ]]; then
                    case "${COMP_WORDS[3]}" in
                        message)
                            if [[ "${COMP_WORDS[4]}" == "send" ]]; then
                                COMPREPLY=($(compgen -W "--msg" -- ${cur}));
                                return 0
                            fi
                        ;;
                    esac
                fi
    esac
}
complete -F _intelmqctl intelmqctl
